# coding: utf-8
from PySide6.QtCore import QPropertyAnimation, Qt, QEasingCurve, QSize, QPoint
from PySide6.QtGui import QShowEvent, QColor
from PySide6.QtWidgets import QVBoxLayout, QListWidgetItem, QHBoxLayout
from qfluentwidgets import SimpleCardWidget, BodyLabel, ListWidget, setFont, isDarkTheme, TransparentToolButton, \
    FluentIcon

from app.common import BookItemModel


class BaseBar(SimpleCardWidget):

    def _normalBackgroundColor(self):
        c = 0 if isDarkTheme() else 255
        return QColor(c, c, c, 150)


class LeftBar(BaseBar):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.verticalLayout = QVBoxLayout(self)
        self.titleLabel = BodyLabel('书名', self)
        self.listWidget = ListWidget(self)
        self.animation = QPropertyAnimation(self, b'pos', self)

        setFont(self.titleLabel, 24)
        self.titleLabel.setContentsMargins(10, 10, 10, 10)
        self.setFixedWidth(300)

        self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.verticalLayout.addWidget(self.titleLabel)
        self.verticalLayout.addWidget(self.listWidget)

        self.animation.setEasingCurve(QEasingCurve.Type.InOutQuart)
        self.animation.setDuration(200)

    def setTitle(self, title: str):
        self.titleLabel.fontMetrics().elidedText(title, Qt.TextElideMode.ElideRight, 260)
        self.titleLabel.setText(title)

    def addModel(self, model: BookItemModel):
        item = QListWidgetItem(model.title)
        item.setSizeHint(QSize(200, 40))
        item.setData(Qt.ItemDataRole.UserRole, model.photo_url)
        self.listWidget.addItem(item)

    def setCurrentIndex(self, index: int):
        self.listWidget.setCurrentRow(index)

    def currentIndex(self) -> int:
        return self.listWidget.currentRow()

    def nextPage(self):
        """
        下一页
        :return:
        """
        currentIndex = self.currentIndex()
        self.listWidget.setCurrentRow(currentIndex + 1)

    def prevPage(self):
        """
        上一页
        :return:
        """
        currentIndex = self.currentIndex()
        self.listWidget.setCurrentRow(currentIndex - 1)

    def showEvent(self, event: QShowEvent):
        self.animation.setStartValue(QPoint(self.x() - self.width() - 10, self.y()))
        self.animation.setEndValue(QPoint(10, self.y()))
        self.animation.start()
        event.accept()

    def hideEvent(self, event):
        self.animation.setStartValue(QPoint(10, self.y()))
        self.animation.setEndValue(QPoint(self.x() - self.width() - 10, self.y()))
        self.animation.start()
        event.accept()

    def _normalBackgroundColor(self):
        c = 0 if isDarkTheme() else 255
        return QColor(c, c, c, 150)


class BottomBar(BaseBar):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.horizontalLayout = QHBoxLayout(self)
        self.menuBtn = TransparentToolButton(FluentIcon.MENU, self)
        self.prevBtn = TransparentToolButton(FluentIcon.LEFT_ARROW, self)
        self.nextBtn = TransparentToolButton(FluentIcon.RIGHT_ARROW, self)


        self.horizontalLayout.addWidget(self.menuBtn)
        self.horizontalLayout.addWidget(self.prevBtn)
        self.horizontalLayout.addWidget(self.nextBtn)

        self.setFixedHeight(50)
